package com.vcc.core.util;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.text.StrBuilder;
import cn.hutool.core.util.IdUtil;
import cn.hutool.crypto.SecureUtil;

import java.util.HashMap;

public class EncryptUtil {


    public static String encryptValue(String value, String salt) {
        HashMap<Object, Object> tempMap = MapUtil.newHashMap();
        tempMap.put("password", Base64.encode(value));
        tempMap.put("salt", salt);
        StrBuilder strBuilder = new StrBuilder();
        strBuilder.append(salt).append("&");
        tempMap.forEach((key, tempValue) -> {
            strBuilder.append(key).append("=").append(tempValue).append("&");
        });
        // 去掉最后一个&
        strBuilder.append(salt);
        String encryptPassword = strBuilder.toString();
        return SecureUtil.sha256(encryptPassword);
    }


    public static String generateSalt() {
        return IdUtil.randomUUID();
    }
}
